<template>
  <a-card :bordered="false" class="card-area">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <!-- 搜索区域 -->
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="名称" :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}">
              <a-input placeholder="请输入名称查询" v-model="queryParam.roleName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="10" :sm="12">
            <a-form-item label="创建时间" :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}">
              <j-date v-model="queryParam.createTime_begin" :showTime="true" date-format="YYYY-MM-DD HH:mm:ss" style="width:45%" placeholder="请选择开始时间" ></j-date>
              <span style="width: 10px;">~</span>
              <j-date v-model="queryParam.createTime_end" :showTime="true" date-format="YYYY-MM-DD HH:mm:ss" style="width:45%" placeholder="请选择结束时间"></j-date>
            </a-form-item>
          </a-col>
          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
            </a-col>
          </span>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator"  style="margin-top: 5px">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>

      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择&nbsp;<a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination1"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="resume(record)" v-if="record.categoryStatus==false">启用</a>
          <a @click="pause(record)" v-if="record.categoryStatus==true">停用</a>
          <a-divider type="vertical" />
          <a @click="handleEdit(record)">编辑</a>
        </span>
       
        <template slot="status" slot-scope="status">
            <a-tag v-if="status==1" color="green">已启用</a-tag>
            <a-tag v-if="status==0" color="orange">已暂停</a-tag>
        </template>
        <span slot="level" slot-scope="categoryLevel">
            {{
                categoryLevel==1 ? "一级分类" : "二级分类"
            }}
        </span>
        <img  style="width:50px;heigth:50px" slot="pic" slot-scope="pic" :src="getImgUrl(pic)" />
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <category-modal ref="modalForm" @ok="modalFormOk"></category-modal>
  </a-card>
</template>

<script>
  import CategoryModal from './modules/CategoryModal'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import JDate from '@/components/jeecg/JDate'
  import { postAction } from '@/api/manage'

  export default {
    name: "CategoryList",
    mixins:[JeecgListMixin],
    components: {
      CategoryModal,
      JDate
    },
    data () {
      return {
        href:window._CONFIG['domianURL']+"/sys/common/static/",
        // 查询条件
        queryParam: {},
        ipagination1:{
        current: 1,
        pageSize: 30,
        pageSizeOptions: ['30', '50'],
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条"
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
        // 表头
        columns: [
          {
            title: '分类名称',
            align:"center",
            dataIndex: 'categoryName'
          },
           {
            title: '分类图标',
            align:"center",
            dataIndex: 'iconImg',
            scopedSlots: { customRender: 'pic' },
          },
          {
            title: '等级',
            align:"center",
            dataIndex: 'categoryLevel',
            scopedSlots:{customRender:"level"}
          },
          {
            title: '状态',
            align:"center",
            dataIndex: 'categoryStatus',
            scopedSlots:{customRender:"status"}
          },
          {
            title: '排序',
            align:"center",
            dataIndex: 'categoryOrder'
          },
          {
            title: '创建时间',
            dataIndex: 'createTime',
            align:"center",
            sorter: true
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: "/adh/shopCategory/list",
          delete: "/adh/shopCategory/delete",
          deleteBatch: "/adh/shopCategory/deleteBatch",
          updateStatus:"/adh/shopCategory/updateStatus",
        },
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      onChangeDate(date, dateString) {
       
      },
       getImgUrl(picUrl){
        if(picUrl&&picUrl.split(",").length>0){
            return this.href + picUrl.split(",")[0];
        }else{
           return "";
        }
      },
      resume(record){
        var that = this;
        this.$confirm({
          title:"确认启用",
          content:"是否启用选中分类?",
          onOk: function(){
            postAction(that.url.updateStatus,{id:record.id,status:"1"}).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.loadData();
                that.onClearSelected();
              }else{
                that.$message.warning(res.message);
              }
            });
          }
        });
      },
      pause(record){
        var that = this;
        this.$confirm({
          title:"确认暂停",
          content:"是否暂停选中分类?",
          onOk: function(){
            postAction(that.url.updateStatus,{id:record.id,status:"0"}).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.loadData();
                that.onClearSelected();
              }else{
                that.$message.warning(res.message);
              }
            });
          }
        });
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>